﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Evan.Common
{
    /// <summary>
    /// Ajax通用基类 注意 ProcessRequest 为虚方法 , 需要重写
    /// </summary>
    public class AjaxHelper : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 虚方法重写即可  不可在此写逻辑代码
        /// </summary>
        /// <param name="context"></param>
        public virtual void ProcessRequest(HttpContext context)
        {

        }

        /// <summary>
        /// 获取传递的参数  <para>排除了 ,[object Object]  字样</para>
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        protected string GetParam(string Name)
        {
            return Evan.Common.CommFun.GetParams(Name).Replace(",[object Object]", "");
        }
        

        /// <summary>
        /// 输出json结果 返回2个值 result (输出succes的值)   returnval (输出str的值)
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <param name="success">是否操作成功,0表示失败;1表示成功</param>
        /// <param name="str">输出字符串</param>
        protected void JsonResult(HttpContext context, int success, string str)
        {
            context.Response.Write("{\"result\" :\"" + success.ToString() + "\",\"returnval\" :\"" + str + "\"}");
            context.Response.End();
        }
        /// <summary>
        /// 输出 Model Json
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <param name="model">输出一个model的json</param>
        protected void JsonResult(HttpContext context, dynamic model)
        {
            string json = Evan.Model.ConvertModel.ConvertToJson(model);
            context.Response.Write(json);
            context.Response.End();
        }

        /// <summary>
        /// 添加 获取默认 model 和 table 参数
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <returns>返回添加的ID</returns>
        protected int AddModel(HttpContext context)
        {
            dynamic model = Model.ConvertModel.ConvertToDynamic(GetParam("model"));
            string table = GetParam("table");
            return new DataBase.DbOperat(table).InsertReturnId(model);
        }
        /// <summary>
        /// 添加 获取默认 model 和 table 参数
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <param name="Model">dynamic 的Model 对象</param>
        /// <returns>返回添加的ID</returns>
        protected int AddModel(HttpContext context,dynamic Model)
        {
            string table = GetParam("table");
            return new DataBase.DbOperat(table).InsertReturnId(Model);
        }

        /// <summary>
        /// 更新 获取默认 model 和 table 参数
        /// </summary>
        /// <param name="context"></param>
        /// <returns>返回是否成功</returns>
        protected bool UpdateModel(HttpContext context)
        {
            dynamic model = Model.ConvertModel.ConvertToDynamic(GetParam("model"));
            string table = GetParam("table");
            return new DataBase.DbOperat(table).Update(model);
        }
        /// <summary>
        /// 更新 获取默认 model 和 table 参数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="Model">dynamic 的Model 对象</param>
        /// <returns>返回是否成功</returns>
        protected bool UpdateModel(HttpContext context,dynamic Model)
        {
            string table = GetParam("table");
            return new DataBase.DbOperat(table).Update(Model);
        }
        
#if false
        
        protected object GetModel(HttpContext context)
        {

            return null;
        }
        protected object GetModel(HttpContext context, int Id)
        {

            return null;
        }

        protected object GetList(HttpContext context,string where)
        {
            return null;
        }

        protected object GetPage(HttpContext context)
        {
            return null;
        }

        protected object Del(HttpContext context)
        {
            return null;
        }
#endif

        //protected object Delete_Virtual(HttpContext context)
        //{
        //    return null;
        //}



        
        /// <summary>
        /// 
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
